package entity.lemming;

import java.awt.Point;
import java.awt.Rectangle;
import java.awt.image.BufferedImage;

import entity.Entity;
import entity.terrain.Terrain;
import level.Level;

/**
 * A Lemming that climbs vertical surfaces.
 * 
 * @author Neil Cowzer
 */
public class Climber extends Lemming {
	
	

	/**
	 * Create an instance of the Climber Lemming.
	 * 
	 * @param x the starting x-coordinate.
	 * @param y the starting y-coordinate.
	 */
	/*@ requires 0 <= x && 0 <= y;
		assignable speed_X && speed_Y && energy;
		ensures speed_X == 0 && speed_Y < 0 && energy == 100;
	 */
	public Climber(int x, int y){
		super("resource/graphics/lemming/climber/climber_1.gif", 15,x, y);
		speed_X = 0;
		speed_Y = -1;
		energy = 100;
	}
	
	/**
	 * Create a new Climber with the given center point
	 * 
	 * @param center point of the Lemming
	 */
	public Climber(Point center){
		super("resource/graphics/lemming/climber/climber_1.gif", 15,center);
		speed_X = 0;
		speed_Y = -1;
		energy = 100;
	}
	
	/**
	 * Checks for a collision with another Entity
	 * 
	 * @param other another Entity eg Terrain, Lemming.
	 * @return true if a collision has occured.
	 */
	public /*@ pure @*/ boolean collision(Lemming other) {
/*		Rectangle me = getBounds();
		if (other instanceof Terrain){
			if (me.intersects(other.getBounds())){
				//return true;
			}
		}
*/
		return false;
	}

	/** Request that this lemming perform the appropiate action/movement. */
	/*@ ensures x == \old(x) &&
	 * 			y = \old(y) + speed_Y &&
				energy == \old(energy) - 1;
	 */
	public void move(Terrain terrain) {
		boolean bash = false;
		Rectangle lemming = getBounds();
		
		if (lemming.intersects(terrain.getBounds())){
			BufferedImage image = ((Terrain)terrain).getImage().getBufferedImage();
			int black = 16777215; // black that represents the background
			try{
				if (speed_X > 0){
					// if the is at least one non- black pixel - dig
					for (int i = getY(); i < getY() + lemming.height; i++){
						int color = image.getRGB(x+lemming.width, i);

						if (color != black){
							bash = true;
							break;
						}

						/* Allow for a little bit of freedom,
						 * if the lemming is "close" to the wall.
						 */ 
						color = image.getRGB(x+lemming.width+1, i);
						if (color != black){
							bash = true;
							break;
						}

						color = image.getRGB(x+lemming.width+2, i);
						if (color != black){
							bash = true;
							break;
						}
					}


					if (bash){
						if (energy == 30){
							for (int i = x; i < x + lemming.width; i++){
								terrain.destroyTerrain(i, y, "basher");
							}
						}
						terrain.destroyTerrain(x+lemming.width, y, "basher");
					} else {
						energy = 1;
					}
				}else{
					// if the is at least one non- black pixel - dig
					for (int i = getY(); i < getY() + lemming.height; i++){
						int color = image.getRGB(x, i);

						if (color != black){
							bash = true;
							break;
						}

						/* Allow for a little bit of freedom,
						 * if the lemming is "close" to the wall.
						 */ 
						color = image.getRGB(x-1, i);
						if (color != black){
							bash = true;
							break;
						}

						color = image.getRGB(x-2, i);
						if (color != black){
							bash = true;
							break;
						}
					}


					if (bash){
						if (energy == 30){
							for (int i = x; i < x + lemming.width; i++){
								terrain.destroyTerrain(i, y, "basher");
							}
						}
						terrain.destroyTerrain(x-1, y, "basher");
					} else {
						energy = 1;
					}
				}
			}catch(ArrayIndexOutOfBoundsException e){}
		}
		
				//update positions
		y= y+speed_Y;
		update();
		energy--;

	}
	
	/** Reverse a Lemmings direction */
	public void turnAround(){
		//speed_Y = -(speed_Y);
		if (speed_X < 0){
			generateFrames("resource/graphics/lemming/climber/climberLeft_1.gif", 15);
		}else{
			generateFrames("resource/graphics/lemming/climber/climber_1.gif", 15);
		}
	}

}
